// 获取应用实例
const app = getApp();

Page({
  data: {
    progress: {
      total: 0,
      outline: 0,
      content: 0,
      format: 0
    },
    outlineList: [],
    tipsList: [],
    referencesList: []
  },

  onLoad() {
    this.loadPageData();
  },

  onPullDownRefresh() {
    this.loadPageData().then(() => {
      wx.stopPullDownRefresh();
    });
  },

  // 加载页面数据
  loadPageData() {
    return Promise.all([
      this.loadProgress(),
      this.loadOutline(),
      this.loadTips(),
      this.loadReferences()
    ]);
  },

  // 加载进度信息
  loadProgress() {
    return app.request({
      url: '/essay/progress',
      method: 'GET'
    }).then(res => {
      if (res.code === 0) {
        this.setData({
          progress: res.data
        });
      }
    }).catch(() => {
      app.showError('获取进度信息失败');
    });
  },

  // 加载大纲信息
  loadOutline() {
    return app.request({
      url: '/essay/outline',
      method: 'GET'
    }).then(res => {
      if (res.code === 0) {
        this.setData({
          outlineList: res.data.list
        });
      }
    }).catch(() => {
      app.showError('获取大纲信息失败');
    });
  },

  // 加载写作建议
  loadTips() {
    return app.request({
      url: '/essay/tips',
      method: 'GET'
    }).then(res => {
      if (res.code === 0) {
        this.setData({
          tipsList: res.data.list
        });
      }
    }).catch(() => {
      app.showError('获取写作建议失败');
    });
  },

  // 加载参考资料
  loadReferences() {
    return app.request({
      url: '/essay/references',
      method: 'GET'
    }).then(res => {
      if (res.code === 0) {
        this.setData({
          referencesList: res.data.list
        });
      }
    }).catch(() => {
      app.showError('获取参考资料失败');
    });
  },

  // 预览参考资料
  previewReference(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/essay-guide/reference/reference?id=${id}`
    });
  }
}); 